package com.taomus.mytools.simplec.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;

public final class FileStreamUtils {
	/**
	 * 获取输入流的内容，UTF-8编码
	 * @param input
	 * @param encoding
	 * @return
	 * @throws IOException
	 */
	public static String getInputStreamContent(InputStream input) throws IOException {
		return getInputStreamContent(input, "UTF-8");
	}
	/**
	 * 获取输入流的内容, 只能获取一次流中的内容
	 * @param input
	 * @param encoding
	 * @return
	 * @throws IOException
	 */
	public static String getInputStreamContent(InputStream input, String encoding) throws IOException {
		StringBuilder sb = new StringBuilder();
		InputStreamReader isr = new InputStreamReader(input, encoding);
		BufferedReader br = new BufferedReader(isr);
		int cline;
		while ((cline = br.read()) != -1) {
			sb.append((char)cline);
		}
		isr.close();
		br.close();
		input.close();
		return sb.toString();
	}
	/**
	 * 获取输入流的字节数据
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public static byte[] getByteContent(InputStream input) throws IOException {
		ByteArrayOutputStream br = new ByteArrayOutputStream();
		byte[] buf = new byte[4096];
		int size;
		while ((size = input.read(buf, 0, buf.length)) != -1) {
			br.write(buf, 0, size);
			br.flush();
		}
		br.close();
		input.close();
		return br.toByteArray();
	}
	/**
	 * 将字节保存到指定的文件中
	 * @param path
	 * @param content
	 * @throws IOException
	 */
	public static void save(String path, byte[] content) throws IOException {
		save(path,new ByteArrayInputStream(content));
	}
	/**
	 * 将字符串保存到指定的文件中
	 * @param path
	 * @param content
	 * @throws IOException
	 */
	public static void save(String path, String content) throws IOException {
		save(path,new ByteArrayInputStream(content.getBytes()));
	}
	/**
	 * 将输入流保存到指定的文件中
	 * @param path
	 * @param input
	 * @throws IOException
	 */
	public static void save(String path, InputStream input) throws IOException {
		FileOutputStream output = new FileOutputStream(new File(path));
		byte[] buf = new byte[4096];
		int size;
		while ((size = input.read(buf, 0, buf.length)) != -1) {
			output.write(buf, 0, size);
			output.flush();
		}
		output.close();
		input.close();
	}
	
	/**
	 * 合并２个输入流
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	public static InputStream streamMerge(InputStream arg0,InputStream arg1){
		SequenceInputStream sequenceInputStream = new SequenceInputStream(arg0,arg1);
		return sequenceInputStream;
	}
	
	/**
	 * 合并多个输入流
	 * @param eum
	 * @return
	 */
	public static InputStream streamMerge(Enumeration<InputStream> eum){
		SequenceInputStream sequenceInputStream = new SequenceInputStream(eum);
		return sequenceInputStream;
	}
	
	
	/**
	 * 根据文件名获取在应用中的路径
	 * @param name
	 * @return
	 * @throws IOException 
	 */
	public static String getFilePathByFileName(String name) throws IOException {
		String path = null;
		try{
			path = Thread.currentThread().getContextClassLoader().getResource(name).getPath();
		}catch(java.lang.NullPointerException ex){
			path = Thread.currentThread().getContextClassLoader().getResource(".").getPath()+name;
			String directory = path.substring(0,path.lastIndexOf("/"));
			File file = new File(path);
			File fileDir = new File(directory);
			if(fileDir.mkdirs() && !file.exists()){
				file.createNewFile();
			}
		}
		return path;
	}
}